package chain

import "fmt"

// 脚本处理链
const (
	ScriptChainDefault = "default"	// 默认处理器链
	ScriptChainMath    = "math"	// 数学脚本处理器链
)

var chainMap = map[string][]ScriptHandler{
	// 默认处理器链
	ScriptChainDefault: {
		&GPTScriptHandler{},
		&Latex2NlHandler{},
		&ShotSplitHandler{},
	},
	// 小数处理器链
	ScriptChainMath: {
		&GPTScriptHandler{},
		&Latex2NlHandler{},
	},
}

func StartScript(id int64, chainType string) error {
	if _, ok := chainMap[chainType]; !ok {
		return fmt.Errorf("invalid chain type: %s", chainType)
	}
	handlers := chainMap[chainType]
	if len(handlers) == 0 {
		return fmt.Errorf("no handler found for chain type: %s", chainType)
	}
	return handlers[0].Handle(id, 0)
}


// ScriptHandler 脚本处理接口
type ScriptHandler interface {
	Handle(id int64, step int) error
}

// BaseScriptHandler 基础脚本处理器
type BaseScriptHandler struct {
	chainType string
}

func (h *BaseScriptHandler) HandleNex(id int64, step int) error {
	if step >= len(chainMap[h.chainType]) {
		return nil
	}
	return chainMap[h.chainType][step].Handle(id, step)
}

// GPTScriptHandler GPT脚本生成器
type GPTScriptHandler struct {
	BaseScriptHandler
}

func (h *GPTScriptHandler) Handle(id int64, step int) error {
	// 2. 调用GPT生成脚本
	fmt.Printf("GPTScriptHandler start. id = %d\n", id)
	return h.HandleNex(id, step+1)
}

// Latex2NlHandler latex转自然语言处理器
type Latex2NlHandler struct {
	BaseScriptHandler
}

func (h *Latex2NlHandler) Handle(id int64, step int) error {
	// 3. latex转自然语言
	fmt.Printf("Latex2NlHandler start. id = %d\n", id)
	return h.HandleNex(id, step+1)
}

// ShotSplitHandler -镜头切分处理器
type ShotSplitHandler struct {
	BaseScriptHandler
}

func (h *ShotSplitHandler) Handle(id int64, step int) error {
	// 4. shot切分
	fmt.Printf("ShotSplitHandler start. id = %d\n", id)
	return h.HandleNex(id, step+1)
}


func GenerateScriptWithGPT(id int64, step int) string {
	// 模拟调用GPT生成脚本
	return fmt.Sprintf("Generated script for ID %d", id)
}
